"""
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from matrix_bot_api.matrix_bot_api import MatrixBotAPI
from matrix_bot_api.mregex_handler import MRegexHandler
from matrix_bot_api.mcommand_handler import MCommandHandler
import time
import sys

USERNAME = "theemacsshibe"  # Bot's username
PASSWORD = "get your own lol"  # Bot's password
SERVER = "https://disroot.org"  # Matrix server URL

naughty_counter = 0

def check_e(room, event):
#    global naughty_counter
#    naughty_counter += 1
    try:
        print("got a fifthglyph!")
        r = event["content"]["body"].replace("e","_").replace("E","_")
        if r.replace("_","") == "":
            room.send_text("{}, you aren't funny.".format(event["sender"]))
        else:
            room.send_text("{}: I'm sorry, your last post contains fifthglyphs so it was withdrawn: \"{}\"".format(event["sender"], r))
    except Exception as e:
        print(e.message)
        pass

def drange(start, stop, step):
    l = []
    for i in range(int(start / step), int(stop / step)):
        l.append(i * step)
    return l

def main():
    global naughty_counter
    print("Connecting...")
    bot = MatrixBotAPI(USERNAME, PASSWORD, SERVER)
    e_handler = MRegexHandler("(?i)e", check_e)
    bot.add_handler(e_handler)
    print("Starting polling...")
    bot.start_polling()
    oldcount = 0
    while True:
        time.sleep(1)

if __name__ == "__main__":
    main()

